const userServ = require('../service/userServ.js')
const JWT = require('../utils/jwt.js')

const userCtrl = {
  // 居民诉求
  webAppeallist: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.webAppeallistServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
      data:res
    }
  },
  // 编辑公告
  editAnnouce: async (ctx)=>{
    let data = ctx.request.body
    await userServ.editAnnouceServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
  // 诉求图片上传
  uploadAppealImgs: async (ctx)=>{
    const {filename} = ctx.file
    // console.log(filename);
    let path = `/upload/${filename}`
    ctx.body = {
      code: 0,
      data:path,
    }
  },
  userUpAppeal: async (ctx)=>{
    let data = ctx.request.body
    await userServ.userUpAppealServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
  userEditInfo: async (ctx)=>{
    let data = ctx.request.body
    await userServ.userEditInfoServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
  wxRegister: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.wxRegisterServ( data)
    try {
      ctx.body = {
        code: 0,
        message: 'success',
        data: res
      }
    } catch (err) {
      console.error('用户登录失败', err)
    }
  },
  weblogin: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.webloginServ( data)
    try {
      ctx.body = {
        code: res.code,
        msg: res.msg,
        data: res.data,
        token : res.token
      }
    } catch (err) {
      console.error('用户登录失败', err)
    }
  },
  wxlogin: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.wxloginServ( data)
    try {
      ctx.body = {
        code: 0,
        message: 'success',
        token: JWT.generate({
          uid: res.uID, // 测试
        }, '1d'),
        data: res
      }
    } catch (err) {
      console.error('用户登录失败', err)
    }
  },
  userEvo:async (ctx) => {
    let data = ctx.request.body
     await userServ.userEvoServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
  userAppeal :async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.userAppealServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
      data:res
    }
  }, 
  userlist: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.userlistServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
      data:res
    }
  },
  userHeal: async (ctx) => {
    let data = ctx.request.body
    const res = await userServ.userHealServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
      data:res
    }
  },
  deleteAppealweb: async (ctx) => {
    let data = ctx.request.body
    await userServ.deleteAppealwebServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
  editAppealstatus: async (ctx) => {
    let data = ctx.request.body
    await userServ.editAppealServ(data)
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  },
}

module.exports  = userCtrl